﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ESF.General;
using ESF.SSO.Core.BusinessLogic;
using ESF.SSO.Core.Entities;
using ESF.Web;
using ESF.SSO.Web.Tools.Modules;

#endregion


public partial class User_SendMessage : ESF.Web.Pages.BasePage
{
    #region Carga Inicial
	
	protected void Page_Load(object sender, EventArgs e)
    {
		if (!IsPostBack)
		{
			lblTitle.Text = Title;

			txtUserToAdd.Focus ();
		}
    }

	#endregion


	#region Funciones

	private void ShowUsersToAdd ()
	{
		GridLoader.LoadGrid<User>(grvUserResults, dpggrvUserResults, 
			CtrlUser.Browse(
				new User()
				{
					Alias = txtUserToAdd.Text,
					Email = txtUserToAdd.Text,
					filFullName = txtUserToAdd.Text,
					IdentificationNumber = txtUserToAdd.Text,
					IsEnabled = true,
					filEasySearch = true
				},
			dpggrvUserResults.PaggingInfo));
	}


	private void SendMessage ()
	{
		if (lstUsersTo.Items.Count == 0)
		{
			JS.AddWarning (Page, "Seleccione al menos un usuario para enviar el mensaje");
			txtUserToAdd.Focus ();
			return;
		}

		if (rblModoEnvio.SelectedValue == "Multiple")
		{
			CtrlUser.SendMessage (ListLoader.GetAllValues (lstUsersTo).ToArray (), AuthenticationModule.SignedInUser, txtSubject.Text, txtMessage.Text);

			JS.AddWarning(Page, string.Format("El mensaje ha sido programado para ser enviado a los {0} destinatarios seleccionados.\nSe le notificará a {1} confirmando el envío", lstUsersTo.Items.Count, AuthenticationModule.SignedInUser.Email));
		}
		else 
		{
			CtrlUser.SendMessageForAll(AuthenticationModule.SignedInUser, true, txtSubject.Text, txtMessage.Text);

			JS.AddWarning (Page, string.Format ("El mensaje ha sido programado para enviarlo a todos los usuarios registrados\nSe le notificará a {0} confirmando el envío", AuthenticationModule.SignedInUser.Email));
		}
	}


	private void Clear ()
	{
		txtMessage.Text = string.Empty;
		txtSubject.Text = string.Empty;
		txtUserToAdd.Text = string.Empty;
		lstUsersTo.Items.Clear ();

		GridLoader.Clear (grvUserResults);
		
		dpggrvUserResults.Visible = false;
		
		rblModoEnvio.SelectedIndex = 0;
		divEditUsersTo.Visible = true;
		txtUserToAdd.Focus();
	}


	private void AddUser ()
	{
		IEnumerable<string> users;
		
		
		users = GridLoader.GetSelectedIdentifiersAsString (grvUserResults);

		if (DataHelper.IsEmpty (users)) 
		{
			JS.AddWarning (Page, "Seleccione al menos un usuario para adicionarlo");
			return;
		}

		foreach (string user in users)
		{
			ListLoader.AddItem (lstUsersTo, user, user.Substring (user.LastIndexOf (" - ") + 3));
		}
	}


	private void RemoveUser ()
	{
		ListLoader.RemoveSelected (lstUsersTo);
	}

	#endregion


	#region Eventos de la Grilla

	protected void chkSelectAll_CheckedChanged(object sender, EventArgs e)
	{
		GridLoader.CheckHeaderAll(grvUserResults, "chkSelectAll", "chkSelect");
	}


	protected void dpggrvUserResults_OnPageChanged()
	{
		ShowUsersToAdd ();
	}

	protected void grvUserResults_RowDataBound(object sender, GridViewRowEventArgs e)
	{
		User user;


		if (e.Row.RowType == DataControlRowType.DataRow)
		{
			user = e.Row.DataItem as User;

			GridLoader.SetHiddenFieldForID(e.Row, string.Format("{0} - {1}", user.calFullName, user.Email));

			(e.Row.FindControl("lblUser") as Label).Text = string.Format("{0} - {1}", user.calFullName, user.Email);
		}
	}

	#endregion


	#region Eventos de los botones

	protected void btnSearchUser_Click(object sender, EventArgs e)
	{
		ShowUsersToAdd ();
	}


	protected void btnSend_Click(object sender, EventArgs e)
	{
		SendMessage();
	}


	protected void btnClear_Click(object sender, EventArgs e)
	{
		Clear ();
	}

	protected void btnAddUserTo_Click(object sender, EventArgs e)
	{
		AddUser ();
	}

	protected void btnRemoveUsersTo_Click(object sender, EventArgs e)
	{
		RemoveUser();
	}

	#endregion


	#region Eventos de Campos

	protected void rblModoEnvio_SelectedIndexChanged(object sender, EventArgs e)
	{
		if (rblModoEnvio.SelectedValue == "Multiple")
		{	
			divEditUsersTo.Visible = true;
			txtUserToAdd.Focus();
		}
		else
		{
			divEditUsersTo.Visible = false;
		}
	}

	#endregion
	
}